package com.glsc.ngateway.shortmsg.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.glsc.ngateway.shortmsg.service.ShortMessageService;
import com.glsc.ngateway.shortmsg.task.ShortMessageSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;

@RestController
public class TestController {

    Logger logger = LoggerFactory.getLogger(TestController.class);

    @Autowired
    RestTemplate restTemplate;
    @Autowired
    private ShortMessageService shortMessageService;

    ObjectMapper mapper = new ObjectMapper();

    @PostMapping(value = "sendShortMessage")
    public void sendShortMessage() {
        try {
            Map<String, Object> request = ShortMessageSupport.getShortMessageParam("资管产品系统工作提醒：国联圆融安享6号集合资产管理计划产品申请划款： 于2021-07-08进行下述划款：提取，金额=111元；请根据资金划款情况，在产品管理系统跟进处理。", "127.0.0.1", "13800138000", "amcproduct");
            logger.info("向短信平台发送消息:{}", request);
            String response = restTemplate.postForObject("http://10.197.49.38:80/imf/api/v1/json", request, String.class);
            logger.info("从短信平台接收到结果:{}", response);
            response = "{\n" +
                    "    \"data\":{\n" +
                    "        \"errorno\":0,\n" +
                    "        \"errorinfo\":\"\",\n" +
                    "        \"datasets\":{\n" +
                    "            \"dataset0\":[\n" +
                    "                {\n" +
                    "                    \"error_info\":\"成功\",\n" +
                    "                    \"error_no\":\"0\"\n" +
                    "                }\n" +
                    "            ]\n" +
                    "        },\n" +
                    "        \"datasetname\":\"dataset0\"\n" +
                    "    },\n" +
                    "    \"code\":20000\n" +
                    "}";
            Map resMap = mapper.readValue(response, Map.class);
            if (resMap.containsKey("code") && ("20000".equals(resMap.get("code").toString()))) {
                try {
                    if (resMap.containsKey("data")) {
                        if (((Map) resMap.get("data")).containsKey("datasets")) {
                            if (((Map) (((Map) resMap.get("data")).get("datasets"))).containsKey("dataset0")) {
                                Map dataset0 = (Map) ((List) ((Map) (((Map) resMap.get("data")).get("datasets"))).get("dataset0")).get(0);
                                if (dataset0.containsKey("error_no") && ("0".equals(dataset0.get("error_no").toString()))) {
                                    System.out.println("ffff");
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    logger.error("短信平台解析成功标志位失败:", e);
                }
            }
            String msgId = shortMessageService.initDbrec("13800138000", "资管产品系统工作提醒：国联圆融安享6号集合资产管理计划产品申请划款： 于2021-07-08进行下述划款：提取，金额=111元；请根据资金划款情况，在产品管理系统跟进处理。", "amproduct", "127.0.0.1");
            logger.info("从短信平台接收到结果:{}" + msgId);
        } catch (Exception e) {
            logger.info("从短信平台接收到结果:{}", e);
        }
    }
}
